//
// This script manages the recording and replaying
//

GA_Recording = false
GA_Name = ""
GA_Player = nil
GA_StartTime = os.clock()

//
// Start recording
//
local function StartRecording( Name )
	GA_Recording = true
	GA_Name = Name
	GA_StartTime = os.clock()
end

//
// End recording
//
local function FinishRecording()
	GA_Recording = false
end

//
// Replay last recording
//
local function Replay()
	StartReplaying()
end

//
// Interface with console commands
//
function Con_StartRecording( ply, com, args )
	if ply:IsSuperAdmin() and args[1] != nil and string.len(tostring(args[1])) > 0 then
		if GA_Recording then
			for _, v in pairs(player.GetAll()) do v:ChatPrint( "Recording '" .. GA_Name .. "' (by " .. GA_Player:Nick() .. ") is still running!" ) end
		else
			StartRecording( tostring(args[1]) )
			GA_Player = ply
			for _, v in pairs(player.GetAll()) do v:ChatPrint( "Started recording '" .. GA_Name .. "'" ) end
		end
	end
end
concommand.Add( "GA_StartRecording", Con_StartRecording )

function Con_FinishRecording( ply, com, args )
	if ply:IsSuperAdmin() then
		if GA_Recording then
			FinishRecording()
			for _, v in pairs(player.GetAll()) do v:ChatPrint( "Finished recording '" .. GA_Name .. "'" ) end
		else
			for _, v in pairs(player.GetAll()) do v:ChatPrint( "There is no recording running at the moment!" ) end
		end
	end
end
concommand.Add( "GA_FinishRecording", Con_FinishRecording )

function Con_Replay( ply, com, args )
	if ply:IsSuperAdmin() then
		if GA_Recording then
			for _, v in pairs(player.GetAll()) do v:ChatPrint( "Can't replay while still recording '" .. GA_Name .. "'!" ) end
		elseif GA_Name == "" then
			for _, v in pairs(player.GetAll()) do v:ChatPrint( "No recording available for replay yet!" ) end
		else
			for _, v in pairs(player.GetAll()) do v:ChatPrint( "Replaying '" .. GA_Name .. "' by " .. GA_Player:Nick() ) end
			Replay()
		end
	end
end
concommand.Add( "GA_Replay", Con_Replay )